package com.zy.edu.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zy.edu.bean.*;
import com.zy.edu.config.SystemParam;
import com.zy.edu.dao.CourseTypeDao;
import com.zy.edu.service.CourseTypeService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zy.edu.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author HuangHaiShui
 * @since 2021-08-18
 */
@Service
@Transactional
public class CourseTypeServiceImpl extends ServiceImpl<CourseTypeDao, CourseType> implements CourseTypeService {
    @Resource
    UserService userService;
    @Override
    public Map saveOrUpdate(CourseType courseType, InputParam inputParam) {
        //当前用户的信息
        User currentUser = userService.selectById(inputParam.getUserId());
        //只有超管可以操作
        if(currentUser.getRoleType().equals(Role.SUPER)) {
            if (StringUtils.isEmpty(courseType.getId())) {
                courseType.setStatus(1);
                this.baseMapper.insert(courseType);
            } else {
                this.baseMapper.updateById(courseType);
            }
            initSystemData();
            return ResponseInfo.success(null);
        }else {
            return ResponseInfo.error("您无权限创建课程");
        }
    }

    @Override
    public Map list(CourseType courseType, InputParam inputParam) {

        EntityWrapper<CourseType> entityWrapper = new EntityWrapper<CourseType>();
        if(!StringUtils.isEmpty(courseType.getName())){
            entityWrapper.like("name",courseType.getName());
        }
        entityWrapper.eq("status",1);
        entityWrapper.orderBy("id",true);

        //查分页
        if(inputParam!=null && inputParam.getPage()!=null && inputParam.getSize()!=null
                && inputParam.getPage()>0 &&  inputParam.getSize()>0){
            Page<CourseType> page = new Page<CourseType>(inputParam.getPage(), inputParam.getSize());
            List<CourseType> tempList = this.baseMapper.selectPage(page,entityWrapper);
            Integer total = this.baseMapper.selectCount(entityWrapper);
            Map<String, Object> resultMap = new HashMap<String, Object>();
            resultMap.put("rows", tempList);
            resultMap.put("total", total);
            return ResponseInfo.success(resultMap);
        }else {//查全部
            List<CourseType> tempList = this.baseMapper.selectList(entityWrapper);
            return ResponseInfo.success(tempList);
        }
    }

    @Override
    public Map dele(CourseType courseType) {
        if(!StringUtils.isEmpty(courseType.getId())){
            //设置为不可用
            courseType.setStatus(0);
            this.baseMapper.updateById(courseType);
            //更新缓存数据
            initSystemData();
        }
        return ResponseInfo.success(null);
    }

    @Override
    public void initSystemData() {
        SystemParam.COURSE_TYPE = new HashMap();
        List<CourseType> typeList = this.selectList(new EntityWrapper<CourseType>()
                .eq("status",1));
        for (CourseType type : typeList) {
            SystemParam.COURSE_TYPE.put(type.getId(),type.getName());
        }
        SystemParam.COURSE_TYPE.put(6,"校本课程");
    }
}
